﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Connection
{
    class ConversationDetailDB : MarshalByRefObject
    {
        private string cmdtext;
        private ConnectDB connectDB = new ConnectDB();

        private MySqlConnection connection = null;
        #region Insert
        public int InsertConversationDetail(string ConversationDetailId, string UserId1, string UserId2)
        {
            string[] userArray = new string[] { UserId1, UserId2 };
            Array.Sort(userArray, StringComparer.InvariantCulture);
            cmdtext = "INSERT INTO dbchat.conversation_detail VALUES('" + ConversationDetailId + "','" + userArray[0] + "','" + userArray[1] + "')";
            return connectDB.QueryData(cmdtext);
        }
        #endregion

        #region Select
        public List<String> SelectConversationId()
        {
            //connectDB.setConnectionString(ref connectionString);
            connection = new MySqlConnection(connectDB.getConnectionString());
            cmdtext = "SELECT ConversationId FROM dbchat.conversation_detail";
            MySqlCommand cmd = new MySqlCommand(cmdtext, connection);
            DataSet ds = new DataSet();
            List<String> dsID = new List<String>();

            try
            {
                //mo ket noi
                connection.Open();
                MySqlDataAdapter adap = new MySqlDataAdapter();
                adap.SelectCommand = cmd;
                adap.Fill(ds);

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    String temp;
                    temp = ds.Tables[0].Rows[i][0].ToString();

                    dsID.Add(temp);
                }

                return dsID;
            }
            catch
            {
                return null;
            }
            finally
            {
                //dong ket noi 
                if (connection != null)
                {
                    connection.Close();
                }
            }
        }

        //Select UserID by Username 
        public String SelectConversationId(string UserId1, string UserId2)
        {
            string[] userArray = new string[] { UserId1, UserId2 };
            Array.Sort(userArray, StringComparer.InvariantCulture);
            connection = new MySqlConnection(connectDB.getConnectionString());
            cmdtext = "SELECT ConversationId FROM dbchat.conversation_detail WHERE UserId1 = '" + userArray[0] + "' AND UserId2 = '" + userArray[1] + "'";
            MySqlCommand cmd = new MySqlCommand(cmdtext, connection);
            DataSet ds = new DataSet();

            try
            {
                //mo ket noi
                connection.Open();
                MySqlDataAdapter adap = new MySqlDataAdapter();
                adap.SelectCommand = cmd;
                adap.Fill(ds);

                String sConversationId;
                sConversationId = ds.Tables[0].Rows[0][0].ToString();



                return sConversationId;
            }
            catch
            {
                return null;
            }
            finally
            {
                //dong ket noi 
                if (connection != null)
                {
                    connection.Close();
                }
            }
        }
        #endregion

        #region Generate Conversation Id
        public String GenerateConversationId()
        {
            List<String> listConversationId = this.SelectConversationId();
            string generatedConversationId;
            do
            {
                generatedConversationId = "";

                var chars = "3F6JRTGEWO88G7S55XCFGDF4H52GHMB9J2965869SG65GTGD7F46X436DUHJXV7J3SDI7VDFBVF9D8G7E46WH5864HY";
                var random = new Random();
                var result = new String(
                    Enumerable.Repeat(chars, 6)
                              .Select(s => s[random.Next(s.Length)])
                              .ToArray());
                generatedConversationId += "CONV" + result;
            } while (listConversationId.Contains(generatedConversationId));
            return generatedConversationId;
        }
        #endregion
    }
}
